package com.qf.mybatis.test;

import com.qf.mybatis.entity.User;
import com.qf.mybatis.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 郑大仙丶
 * @version 1.0
 * @date 2021/3/16 14:48
 * 修改和删除user表数据
 */
public class Demo12 {

    @Test
    public void test() throws IOException {
        // inputStream
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        // builder
        // factory

        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        // sqlSession
        SqlSession sqlSession = factory.openSession();

        // 使用sqlSession获取UserMapper对应的代理实现类(MyBatis基于JDK动态代理实现)
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);

        // 调用
        User user = new User();
        user.setId(7);
        user.setName("实娃星哥刘能");
        user.setUserAge(333);
        userMapper.updateById(user);


        userMapper.deleteById(8);
        userMapper.deleteById(9);
        userMapper.deleteById(10);


        // 提交事务
        sqlSession.commit();
        sqlSession.close();

    }

}
